﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxSplitter;
using System.Drawing;

namespace OnlineDovizOtomasyonu
{
    public partial class hesapdefteri : System.Web.UI.Page
    {
        Fonksiyon Sistem = new Fonksiyon();
        Formatlar Formatlar = new Formatlar();
        string Yil, Ay, Gun, Saat, Dakika, Saniye;
        DateTime YeniTarih, YeniTarihLoad;
        protected void Page_Load(object sender, EventArgs e)
        {
            ASPxSplitter Splt = (ASPxSplitter)Master.FindControl("OrtaBolunmusPanel");
            Splt.Panes.GetByName("OrtaSolPanel").Collapsed = true;
            
            
            if (Page.IsPostBack != true)
            {
                txtTarih.Value = DateTime.Today;
                Saat = DateTime.Now.Hour.ToString();
                Dakika = DateTime.Now.Minute.ToString();
                Saniye = DateTime.Now.Second.ToString();
                Yil = DateTime.Now.Year.ToString();
                Ay = DateTime.Now.Month.ToString();
                Gun = DateTime.Now.Day.ToString();
                YeniTarihLoad = Convert.ToDateTime(Gun + "." + Ay + "." + Yil + " " + Saat + ":" + Dakika + ":" + Saniye);
                ASPxDateEdit1.Value = YeniTarihLoad;
                if (string.IsNullOrEmpty((string)Session["webingMG"]))
                {
                    
                }
                else
                {
                    txtMusteriGroup.SelectedIndex = Convert.ToInt32(Session["webingMG"]);
                    ToplamCek();
                }
                if (string.IsNullOrEmpty((string)Session["webingMK"]))
                {

                }
                else
                {
                    txtMusteriKodu.SelectedIndex = Convert.ToInt32(Session["webingMk"]);
                    ToplamCek();
                }
            }
            
            //if (Session["webingMK"] != null)
            //{
            //    txtMusteriKodu.Value = Convert.ToInt32(Session["webingMK"]);
            //}

        }

        void Kaydet()
        {
            SqlConnection baglanti = Sistem.baglan();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = baglanti;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SP_WEBINGDOVIZ_DEFTER_EKLE";
            cmd.Parameters.Add("MUSTERIKODU", txtMusteriKodu.Value);
            cmd.Parameters.Add("TARIH", txtTarih.Value);
            cmd.Parameters.Add("TIPI", rdoSecim.Value);
            decimal TL=0, USD=0, EURO=0, SAR=0, SRY=0;
            if (txtTL.Value==null)
            {
                TL = 0;
            } 
            else
            {
                TL=Convert.ToDecimal(txtTL.Text);
            }
            if (txtUSD.Value == null)
            {
                USD = 0;
            }
            else
            {
                USD = Convert.ToDecimal(txtUSD.Text);
            }
            if (txtEURO.Value == null)
            {
                EURO = 0;
            } 
            else
            {
                EURO = Convert.ToDecimal(txtEURO.Text);
            }
            if (txtSAR.Value==null)
            {
                SAR = 0;
            }
                else
            {
                SAR = Convert.ToDecimal(txtSAR.Text);
            }
            if (txtSRY.Value == null)
            {
                SRY = 0;
            }
            else
            {
                SRY = Convert.ToDecimal(txtSRY.Text);
            }
            cmd.Parameters.Add("TL", TL.ToString().Replace(",", "."));
            cmd.Parameters.Add("USD", USD.ToString().Replace(",", "."));
            cmd.Parameters.Add("EURO", EURO.ToString().Replace(",", "."));
            cmd.Parameters.Add("SAR", SAR.ToString().Replace(",", "."));
            cmd.Parameters.Add("SRY", SRY.ToString().Replace(",", "."));
            cmd.Parameters.Add("ACIKLAMA", txtAciklama.Text);
            cmd.Parameters.Add("SAAT", ASPxDateEdit1.Value);
            cmd.Parameters.Add("MUSTERIADI", txtMusteriKodu.Text);
            cmd.ExecuteNonQuery();
            Temizle();
        }

        protected void btnKaydet_Click(object sender, EventArgs e)
        {
            Kaydet();
            
        }

        void Temizle()
        {
            txtTL.Text = "";
            txtUSD.Text = "";
            txtEURO.Text = "";
            txtSAR.Text = "";
            txtSRY.Text = "";
            txtAciklama.Text = "";
            ListeAlacak.DataBind();
            ListeVerecek.DataBind();
            ToplamCek();
        }

        protected void ListeAlacak_BeforePerformDataSelect(object sender, EventArgs e)
        {
            //ControlParameter controlParameter = (ControlParameter)sdAlacakVeri.SelectParameters[0];
            //controlParameter.ControlID = txtMusteriKodu.UniqueID;
        }

        protected void txtMusteriKodu_Load(object sender, EventArgs e)
        {
            //txtMusteriKodu = (ASPxComboBox)sender;
        }

        protected void txtMusteriKodu_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["webingMK"] = txtMusteriKodu.Value;
            ToplamCek();
        }

        protected void btnDuzenle_Click(object sender, EventArgs e)
        {
            
        }

        protected void ListeAlacak_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            
            
        }

        protected void ListeAlacak_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableDataCellEventArgs e)
        {
            //if (e.DataColumn.FieldName != "TL") return;
            //{
            //    decimal TL = (decimal)e.GetValue("TL");
            //    if (TL > 0)
            //    {
            //        e.Cell.Font.Bold = true;
            //    }
            //}

        //    if (e.DataColumn.FieldName != "USD") return;
        //    {
        //        decimal USD = (decimal)e.GetValue("USD");
        //        if (USD > 0)
        //        {
        //            e.Cell.Font.Bold = true;
        //        }
        //    }

        //    if (e.DataColumn.FieldName != "EURO") return;
        //    {
        //        decimal EURO = (decimal)e.GetValue("EURO");
        //        if (EURO > 0)
        //        {
        //            e.Cell.Font.Bold = true;
        //        }
        //    }

        //    if (e.DataColumn.FieldName != "SAR") return;
        //    {
        //        decimal SAR = (decimal)e.GetValue("SAR");
        //        if (SAR > 0)
        //        {
        //                e.Cell.Font.Bold = true;
        //        }
        //    }


        //    if (e.DataColumn.FieldName != "SRY") return;
        //    {
        //        decimal SRY = (decimal)e.GetValue("SRY");
        //        {
        //            e.Cell.Font.Bold = true;
        //        if (SRY > 0)
        //        }
        //    }
            
        }

        protected void btnTemizle_Click(object sender, EventArgs e)
        {
            Temizle();
        }


        void ToplamCek()
        {
            SqlConnection con = Sistem.baglan();
            SqlCommand komut = new SqlCommand();
            komut.Connection = con;
            komut.CommandText = "Select * From VWKOHESAP WHERE MUSTERIKODU=" + Session["webingMK"];
            SqlDataReader dr = komut.ExecuteReader();
            while (dr.Read())
            {
                txtTTl.Text = dr["TTL"].ToString();
                txtTUSD.Text = dr["TUSD"].ToString();
                txtTEURO.Text = dr["TEURO"].ToString();
                txtTSAR.Text = dr["TSAR"].ToString();
                txtTSRY.Text = dr["TSRY"].ToString();
            }
            if (Convert.ToDecimal(txtTTl.Text) > 0)
            {
                txtTTl.BackColor = Color.FromArgb(187, 255, 187);
            }
            else
            {
                txtTTl.BackColor = Color.FromArgb(255, 170, 170);
            }
            if (Convert.ToDecimal(txtTUSD.Text) > 0)
            {
                txtTUSD.BackColor = Color.FromArgb(187, 255, 187);
            }
            else
            {
                txtTUSD.BackColor = Color.FromArgb(255, 170, 170);
            }
            if (Convert.ToDecimal(txtTEURO.Text) > 0)
            {
                txtTEURO.BackColor = Color.FromArgb(187, 255, 187);
            }
            else
            {
                txtTEURO.BackColor = Color.FromArgb(255, 170, 170);
            }
            if (Convert.ToDecimal(txtTSAR.Text) > 0)
            {
                txtTSAR.BackColor = Color.FromArgb(187, 255, 187);
            }
            else
            {
                txtTSAR.BackColor = Color.FromArgb(255, 170, 170);
            }
            if (Convert.ToDecimal(txtTSRY.Text) > 0)
            {
                txtTSRY.BackColor = Color.FromArgb(187, 255, 187);
            }
            else
            {
                txtTSRY.BackColor = Color.FromArgb(255, 170, 170);
            }
        }

        protected void btnYenile_Click(object sender, EventArgs e)
        {
            ListeVerecek.DataBind();
            ListeAlacak.DataBind();
            ToplamCek();
        }

        protected void txtMusteriGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["webingMG"] = txtMusteriGroup.Value;
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            txtSaat.Text = DateTime.Now.ToLongTimeString();
        }

        protected void txtTarih_SelectionChanged(object sender, EventArgs e)
        {
            Saat = DateTime.Now.Hour.ToString();
            Dakika = DateTime.Now.Minute.ToString();
            Saniye = DateTime.Now.Second.ToString();
            DateTime SecilenTarih = Convert.ToDateTime(txtTarih.Value);
            Yil = SecilenTarih.Year.ToString();
            Ay = SecilenTarih.Month.ToString();
            Gun = SecilenTarih.Day.ToString();
            YeniTarih = Convert.ToDateTime(Gun + "." + Ay + "." + Yil + " " + Saat + ":" + Dakika + ":" + Saniye);
            ASPxDateEdit1.Value = YeniTarih;
        }

        protected void txtTarih_ValueChanged(object sender, EventArgs e)
        {
            
        }

       

        
       

        
        
    }
}